package com.hengxinguotong.zhihuichengjian.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ScrollView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.HookMonitorAdapter;
import com.hengxinguotong.zhihuichengjian.ui.app.BaseFragment;
import com.hengxinguotong.zhihuichengjian.bean.HookBeanRes;
import com.hengxinguotong.zhihuichengjian.bean.HookInfoRes;
import com.hengxinguotong.zhihuichengjian.bean.Video;
import com.hengxinguotong.zhihuichengjian.bean.VideoRes;
import com.hengxinguotong.zhihuichengjian.bean.VideoValue;
import com.hengxinguotong.zhihuichengjian.ui.NVRVideoActivity;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.pullrefresh.PullToRefreshBase;
import com.hengxinguotong.zhihuichengjian.widget.pullrefresh.PullToRefreshScrollView;
import com.hengxinguotong.zhihuichengjian.widget.view.NotScrollListview;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/14.
 */

public class HookMonitorFragment extends BaseFragment {

    private View rootView = null;

    @BindView(R.id.hook_sv)
    PullToRefreshScrollView hookSv;
    @BindView(R.id.hook_time)
    HXTextView hookTime;
    @BindView(R.id.hook_height)
    HXTextView hookHeight;
    @BindView(R.id.hook_range)
    HXTextView hookRange;
    @BindView(R.id.video_lv)
    NotScrollListview videoLv;
//    @BindView(R.id.address_tv)
//    HXTextView addressTv;

    private HookBeanRes.HookValue.HookBean hookBean;
    private VideoRes videoRes;
    private HookMonitorAdapter adapter;
    private List<Video> videoList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_hook_monitor, null);
            ButterKnife.bind(this, rootView);
            hookBean = (HookBeanRes.HookValue.HookBean) getArguments().get("hookBean");
            initData();
            initView();

        } else {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    private void initList(){
        adapter = new HookMonitorAdapter(getActivity(), videoList);
        videoLv.setAdapter(adapter);
        videoLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), NVRVideoActivity.class);
                intent.putExtra("videoRes", videoRes);
                intent.putExtra("position", 0);
                intent.putExtra("authority", authority);
                startActivity(intent);
            }
        });
    }

    private void initData(){
        String nvrStr = getArguments().getString("nvrStr");
        videoRes = new VideoRes();
        VideoValue videoValue = new VideoValue();
        videoValue.setNvrStr(nvrStr);
        videoList = hookBean.getData();
        videoValue.setCameraList(videoList);
        videoRes.setValue(videoValue);
        initList();
    }

    private void initView(){
//        addressTv.setText(hookBean.getName());

        hookSv.setMode(PullToRefreshScrollView.Mode.PULL_DOWN_TO_REFRESH);
        hookSv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                getHookInfo("加载中…");
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {

            }
        });
    }

    public static HookMonitorFragment newInstance(HookBeanRes.HookValue.HookBean hookBean, String nvrStr) {
        HookMonitorFragment fragment = new HookMonitorFragment();
        Bundle args = new Bundle();
        args.putSerializable("hookBean", hookBean);
        args.putSerializable("nvrStr", nvrStr);
        fragment.setArguments(args);
        return fragment;
    }

    private void getHookInfo(String alert) {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("id", hookBean.getId());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData(alert, getActivity(), "/hook/getLatestRealTime/" + SPUtil.getString(getActivity(),"uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    HookInfoRes beanRes = gson.fromJson(json, HookInfoRes.class);
                    if(beanRes.getStatus() == 1){
                        process(beanRes);
                    }else if(beanRes.getStatus() == 0){
                        Toast.makeText(getActivity(), beanRes.getMsg(), Toast.LENGTH_SHORT).show();
                    }

                }
            }

            @Override
            public void onFailure(String json) {
                Toast.makeText(getActivity(), json, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void process(HookInfoRes beanRes){
        if(null != beanRes.getValue()){
            hookTime.setText(beanRes.getValue().getDate());
            hookHeight.setText(beanRes.getValue().getHeight());
            hookRange.setText(beanRes.getValue().getExtent());
        }
    }

    @Override
    public void onLazyLoad() {
        getHookInfo("加载中…");
    }
}
